package marksys.free.com.marksys;

import android.util.Log;

import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.Objects;

import dagger.android.AndroidInjector;
import dagger.android.support.DaggerApplication;
import marksys.free.com.marksys.di.DaggerAppComponent;
import marksys.free.com.marksys.ui.main.MainActivity;
import marksys.free.com.marksys.utils.SharedPreferencesUtils;
import timber.log.Timber;

/**
 * Created by zlinsun on 2018/8/23.
 * hah
 */
public class App extends DaggerApplication {

    @Override
    public void onCreate() {
        super.onCreate();
        SharedPreferencesUtils.init(this);
        if (BuildConfig.DEBUG) {
            Timber.plant(new Timber.DebugTree());
        }
        initBugly();
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable throwable) {
                Log.e(TAG, "uncaughtException: ", throwable);
                Log.e(TAG, "uncaughtException: ", throwable);

            }
        });
    }

    void initBugly() {
        Beta.enableHotfix = false;
        Beta.smallIconId = R.mipmap.ic_launcher_round;
        Beta.largeIconId = R.mipmap.ic_launcher_round;
        Beta.defaultBannerId = R.mipmap.ic_launcher;
        Beta.canShowUpgradeActs.add(MainActivity.class);
        Beta.enableNotification = false;
        Beta.autoCheckUpgrade = true;
        Beta.upgradeCheckPeriod = 3 * 1000;
        if ("beta".equals(BuildConfig.BUILD_TYPE)) {
            Beta.autoDownloadOnWifi = true;
        }
        if (BuildConfig.DEBUG) {
            Beta.showInterruptedStrategy = true;
        }
        Beta.canShowApkInfo = true;
        //Beta.downloadListener = downLoadListener;
        //Beta.tipsDialogLayoutId = R.layout.tips_dialog;
        //Beta.upgradeDialogLayoutId = R.layout.upgrade_dialog;
        CrashReport.UserStrategy userStrategy = new CrashReport.UserStrategy(this);
        userStrategy.setAppChannel(BuildConfig.BUILD_TYPE);
//        userStrategy.setCrashHandleCallback(crashCallBack);
        Bugly.setIsDevelopmentDevice(this, BuildConfig.DEBUG);
        Bugly.init(getApplicationContext(), "aa85fa431a", true);
    }

    private static final String TAG = "App";

    @Override
    protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
        return DaggerAppComponent.builder().application(this).build();
    }
}
